﻿using FacebookWrapper;
using FacebookWrapper.ObjectModel;

namespace Desktop_Facebook
{
    public delegate void LoginEventHandler(object sender, LoginEventArgs e);

    internal class Session
    {
        private static Session m_Instance = null;

        public static Session Instance
        {
            get
            {
                if (m_Instance == null)
                {
                    m_Instance = new Session();
                }

                return m_Instance;
            }
        }

        public User LoggedInUser { get; set; }

        public event LoginEventHandler LoggedIn;

		public LoginResult LoginResult { get; set; }

		private Session() 
        { 
        }

        public void Login(bool i_AutoLogin)
        {
            if (i_AutoLogin == true)
            {
                LoginResult = FacebookService.Connect(ApplicationSettings.Instance.AccessToken);
            }
            else
            {
                LoginResult = FacebookService.Login(
                    "337106723005201",
                    "read_mailbox",
                    "friends_birthday",
                    "email",
                    "user_events",
                    "read_stream",
                    "user_likes",
                    "friends_likes");
                ApplicationSettings.Instance.AccessToken = LoginResult.AccessToken;
            }

			if (string.IsNullOrEmpty(LoginResult.ErrorMessage))
            {
				LoggedInUser = LoginResult.LoggedInUser;
            }

            if (LoggedIn != null)
            {
				LoggedIn.Invoke(this, new LoginEventArgs { Result = LoginResult });
            }
        }
    }
}
